<?php
defined('IN_TS') or die('Access Denied.');

//判断用户登录
$userid = aac('user') -> isLogin();

//判断发布者状态
if(aac('user')->isPublisher()==false) tsNotice('不好意思，你还没有权限发布内容！');


switch ($ts) {
	case "" :
		//if ($TS_APP['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
			//tsNotice('系统设置不允许会员发文档！');
		//}

		$groupid = intval($_GET['groupid']);
		$dutyid = intval($_GET['dutyid']);


		if($_SESSION['tsuser']['groupid']==''){
			if($userid != 0){
				$arr1 = 'userid='.$userid;
				$myDuty = aac('duty')->findAll('duty',$arr1,'begintime desc',null,1);
				if($myDuty){
					$_SESSION['tsuser']['groupid']=$myDuty[0]['groupid'];
				}else{
					$_SESSION['tsuser']['groupid']=$arrGroup[0]['groupid'];
				}

			}else{
				$_SESSION['tsuser']['groupid']=$arrGroup[0]['groupid'];
			}
		}

		if($groupid){
			$strGroup = aac('group')->getOneGroup($groupid);
			// 话题类型
			$arrGroupType =  aac('group')->findAll ( 'group_topic_type', array (
				'groupid' => $groupid
			) );
			$isGroupUser = aac('group')->findCount ( 'group_user', array (
				'userid' => $userid,
				'groupid' => $groupid
			) );

			if ($strGroup ['ispost'] == 1 && $isGroupUser == 0 && $userid != $strGroup ['userid']) {
				tsNotice ( "本小组只允许小组成员发贴，请加入小组后再发帖！" );
			}
		}

		$title = '发布文档';
		$uploadurl = tsUrl('article','add',array('ts'=>'do'));
		include  template('add');
		break;

	case "do" :


		$groupid = intval($_POST['groupid']);
		$dutyid = intval($_POST['dutyid']);
		$title = trim($_POST['title']);
		$content = tsClean($_POST['content']);
		$tag = tsClean($_POST['tag']);
		$addtime = date('Y-m-d H:i:s');
		$isnotice = intval($_POST['isnotice']);
		$url =  trim($_POST['url']);
		$typeid = intval ( $_POST ['typeid'] );

		if (intval($TS_USER['isadmin']) == 0) {
			// 过滤内容开始
			if($title){
				aac('system') -> antiWord($title);
			}

			aac('system') -> antiWord($content);
			aac('system') -> antiWord($tag);
			// 过滤内容结束
		}

		if ($isnotice == ''){
			$isnotice =0;
		}

		if ($url){
			if(!$title){
				tsNotice("发布外链时标题不能为空！");
			}

		}

		if($url){
			$content ='';
		}

		if ($content == ''&& $url==''){
			tsNotice("内容和外链不能同时为空！");
		}




		//1审核后显示0不审核
		if ($TS_APP['isaudit'] == 1) {
			$isaudit = 1;
		} else {
			$isaudit = 0;
		}


		$arr =array(
			'userid' => $userid,
			'locationid' => aac('user') -> getLocationId($userid),
			'groupid' => $groupid,
			'dutyid' => $dutyid,
			'content' => $content,
			'isaudit' => $isaudit,
			'isnotice' => $isnotice,
			'url' => $url,
			'addtime' => time(),
			'uptime' => time());

		if($title){
			$arr['title']=$title;
		}

		if($typeid){
			$arr['typeid']=$typeid;
		}


        $articleid = $new['article'] -> create('article', $arr );

/*
		$img_path = getTextPhotos(tsDecode($content));
		$count = count($img_path);
		$img_path = $img_path[$count-1];

		//上传图片开始
		if($img_path){
			$arrUpload = tsUploadPhotoUrl($img_path, $articleid, 'article', array('jpg', 'gif', 'png', 'jpeg'));
			if ($arrUpload) {
				$new['article'] -> update('article', array(
					'articleid' => $articleid
				), array(
					'path' => $arrUpload['path'],
					'photo' => $arrUpload['url']
				));
			}
			// 上传图片结束
		}*/


		// 统计小组下文档数并更新
		$count_article = $new ['article']->findCount ( 'article', array (
			'groupid' => $groupid
		) );
		$new ['article']->update ( 'group', array (
			'groupid' => $groupid
		), array (
			'count_article' => $count_article,
			'uptime' => time ()
		) );

		//处理相关的服务记录
		if($dutyid != ''){
			$new ['article']->update ( 'duty', array (
				'dutyid' => $dutyid
			), array (
				'articleid' => $articleid,
			) );
		}

		// 处理标签
		aac('tag') -> addTag('article', 'articleid', $articleid, $tag);

		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts']);

		// 微信群通知	
		$url = tsUrl('article','show',array('id'=>$articleid));
		$username =  $GLOBALS['TS_USER']['username'];
		$arrGroup = aac('group')->getOneRawGroup($groupid);
		$msg = "【文档发布】：{$username}在{$arrGroup['groupname']} 发布了文档[{$title}]，".
			"快去看看吧 $url";
		aac('wechat')->sendMsgToGroup($groupid, $msg);

		tsNotice("提交成功！建议分享到朋友圈，吸引你的好友加入微澜",'点击查看',tsUrl('article','show',array('id'=>$articleid)),'true');


		/*echo json_encode(array(
			'url'=>tsUrl('article', 'show', array('id' => $articleid))
		));*/

		break;
}
